<template>
	<view class="user-coupon-use" style="padding-top: 1rpx;" :style="{background:pageColor}">

		<view @tap.stop="$util.goUrl({url:`/user/pages/coupon/detail?id=${item.id}`})" class="list-item rel fill-base"
			v-for="(item,index) in list.data" :key="index">
			<view class="bg abs" :style="{background:primaryColor}"></view>
			<view class="flex-center rel" style="z-index: 2;">
				<view class="item-title flex-center c-base abs" :class="[{'underline':item.use_scene}]">
					{{item.use_scene==1?'线下核销':'线上抵扣'}}
				</view>
				<view class="item-status flex-center c-base abs" :style="{color:primaryColor}">
					<view class="bg abs" :style="{background:primaryColor}"></view>
					待使用
				</view>
				<view class="item-price flex-center flex-column c-base rel"
					:style="{background:`linear-gradient( 180deg, ${color} 0%, ${primaryColor} 100%)`}">
					<view class="dot-item abs fill-base radius left"></view>
					<view class="dot-item abs fill-base radius right"></view>
					<view class="flex-y-baseline f-sm-title text-bold">¥<view style="font-size:48rpx">{{item.discount}}
						</view>
					</view>
					<view class="mt-sm f-icontext">{{item.type==0?`满${item.full}元可用`:'无门槛'}}</view>
				</view>
				<view class="flex-1 ml-lg" style="color:#1F1F1F">
					<view class="f-mini-title c-title text-bold max-400 ellipsis" style="margin: 5rpx 0;">
						{{item.title}}
					</view>
					<view class="flex-between pr-lg">
						<view>
							<view class="f-icontext">共{{item.num}}张</view>
							<view class="flex-y-center f-icontext">有效期至<view style="margin-left: 14rpx;">
									{{item.start_time.split(' - ')[1]}}
								</view>
							</view>
						</view>
						<view @tap.stop="toUse(index)" class="use-btn flex-center f-caption c-base radius"
							:style="{background:primaryColor}">
							立即使用
						</view>
					</view>
					<view class="flex-y-center f-ms-little mt-sm" style="color: #ADADAD;">使用规则<i
							class="iconfont iconxiangxiazhankai"></i>
					</view>
				</view>
			</view>
		</view>


		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor type="COUPON" v-if="!loading&&list.data.length<=0&&list.current_page==1" :isCenter="true"></abnor>

		<view class="space-max-footer"></view>

		<fix-bottom-button @confirm="toConfirm" :text="[{text:'不使用卡券',type:'confirm'}]" bgColor="#fff"
			v-if="list.data&&list.data.length>0">
		</fix-bottom-button>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				color: '',
				param: {
					page: 1,
					coach_id: ''
				},
				list: {
					data: []
				},
				loading: true
			}
		},
		computed: mapState({}),
		async onLoad() {
			this.coach_id = this.$util.getPage(-1).options.id
			await this.initIndex()
			this.$util.setNavigationBarColor({
				bg: this.primaryColor
			})
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		methods: {
			...mapMutations(['']),
			async initIndex(refresh = false) {
				this.color = this.$util.rgbColor(this.primaryColor)
				await this.getList()
				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			async getList() {
				let {
					list: oldList,
					param,
					coach_id
				} = this
				param.coach_id = coach_id
				let newList = await this.$api.order.couponList(param);
				newList.data.map(item => {
					item.is_show = false
				})

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			toUse(index) {
				let {
					id
				} = this.list.data[index]
				this.toConfirm(id)
			},
			toConfirm(id = 0) {
				let is_remember = id ? 0 : 1
				this.$util.getPage(-1).orderInfo.coupon_id = id
				let {
					payList,
					payInd
				} = this.$util.getPage(-1)
				let {
					id: payId
				} = payList[payInd]
				if (payId == 4) {
					payInd = payList.findIndex(item => {
						return item.id == 1
					})
					this.$util.getPage(-1).payInd = payInd
					is_remember = 0
				}
				this.$util.getPage(-1).orderInfo.is_remember = is_remember
				this.$util.back()
				setTimeout(() => {
					this.$util.goUrl({
						url: 1,
						openType: 'navigateBack'
					})
				}, 1000)
			}
		}
	}
</script>


<style lang="scss">
	.user-coupon-use {
		.list-item {
			height: 190rpx;
			margin: 20rpx 25rpx 0 25rpx;
			border-radius: 18rpx;

			.bg {
				opacity: 0.1;
				border-radius: 18rpx;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				z-index: 1;
			}


			.item-title {
				top: 0;
				left: 0;
				width: 100rpx;
				padding: 4rpx 0;
				font-size: 20rpx;
				background: linear-gradient(235deg, #FF9E6C 0%, #FF5844 100%);
				border-radius: 16rpx 0 16rpx 0;
				z-index: 3;
			}

			.item-title.underline {
				background: linear-gradient(235deg, #FF9E6C 0%, #FF447F 100%);
			}

			.item-status {
				top: 0;
				right: 0;
				width: 90rpx;
				padding: 3rpx 0;
				font-size: 20rpx;
				z-index: 3;

				.bg {
					opacity: 0.1;
					border-radius: 0 16rpx 0 16rpx;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					z-index: 1;
				}
			}


			.item-price {
				width: 200rpx;
				height: 190rpx;
				border-radius: 18rpx;
				overflow: hidden;

				.dot-item {
					width: 24rpx;
					height: 24rpx;
					top: 83rpx;
				}

				.dot-item.left {
					left: -12rpx;
				}

				.dot-item.right {
					right: -12rpx;
				}
			}

			.iconxiangxiazhankai {
				transform: scale(0.8);
			}

			.use-btn {
				width: 116rpx;
				height: 56rpx;
				transform: rotateZ(360deg);
			}
		}

	}
</style>